<?php


namespace app\controller;


use app\service\OrderService;
use app\utils\Datetime;
use think\facade\Request;

class OrderController
{

    /**
     * 根据订单uuid获取订单详情
     */
    public function queryOrderByUuid() {
        $param['uuid'] = Request::param('uuid');
        return json(OrderService::queryOrderByUuid($param));
    }

    /**
     * 新增订单
     */
    public function createOrder() {
        $param['goods_uuid'] = Request::param('goods_uuid');
        $param['user_uuid'] = Request::param('user_uuid');
        $param['price'] = Request::param('price');
        $param['receive_address'] = Request::param('receive_address');
        $param['user_phone'] = Request::param('user_phone');
        $param['create_time'] = Datetime::today();
        $param['status'] = 1;
        $param['type'] = Request::param('type');

        $chars = md5(uniqid(mt_rand(), true));
        $param['uuid'] = substr ( $chars, 0, 8 ) . '-'
            . substr ( $chars, 8, 4 ) . '-'
            . substr ( $chars, 12, 4 ) . '-'
            . substr ( $chars, 16, 4 ) . '-'
            . substr ( $chars, 20, 12 );

        return json(OrderService::createOrder($param));
    }

    /**
     * 订单付款
     */
    public function payOrder() {
        $param['uuid'] = Request::param('uuid');
        $param['pay_time'] = Datetime::today();

        return json(OrderService::payOrder($param));
    }

    /**
     * 订单发货
     */
    public function deliverOrder() {
        $param['uuid'] = Request::param('uuid');
        $param['deliver_time'] = Datetime::today();

        return json(OrderService::deliverOrder($param));
    }

    /**
     * 订单收货
     */
    public function receiveOrder() {
        $param['uuid'] = Request::param('uuid');
        $param['receive_time'] = Datetime::today();

        return json(OrderService::receiveOrder($param));
    }

    /**
     * 订单取消
     */
    public function deleOrder() {
        $param['uuid'] = Request::param('uuid');
        $param['dele_reason'] = Request::param('dele_reason');
        $param['dele_time'] = Datetime::today();

        return json(OrderService::deleOrder($param));
    }

    /**
     * 返回对应状态对应类型全部订单列表（由新到旧）
     */
    public function listAllOrder() {
        $param['status'] = Request::param('status');
        $param['type'] = Request::param('type');
        $param['startPage'] = Request::param('startPage');
        $param['endPage'] = Request::param('endPage');
        return json(OrderService::listAllOrder($param));
    }

    /**
     * 返回对应状态对应类型个人订单列表（由新到旧）
     */
    public function listAllUserOrder() {
        $param['user_uuid'] = Request::param('user_uuid');
        $param['status'] = Request::param('status');
        $param['type'] = Request::param('type');
        $param['startPage'] = Request::param('startPage');
        $param['endPage'] = Request::param('endPage');

        return json(OrderService::listAllUserOrder($param));
    }
}
